package com.icss.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.icss.model.Item;
import com.icss.model.Manager;
import com.icss.model.User;
import com.icss.service.UserService;
import com.icss.util.ValidateCode;

@Controller
@RequestMapping("uc")
public class UserController {
	@Autowired
	UserService us;
	/*
	 * 进入登录页面管理员
	 */
	@RequestMapping("entry")
	public String entry() {
		return "mLogin";
	}
	/*
	 * 去首页
	 */
	@RequestMapping("index")
	public String index(Map<String, Object>map) {
		List<Item> findByTe = us.findByTe(15);
		map.put("telist", findByTe);
		return "index";
	}
	/*
	 * 退出登录
	 */
	@RequestMapping("breakIndex")
	public String breakIndex(HttpServletRequest request) {
		 	HttpSession session = request.getSession();
	        session.invalidate();
		return "redirect:/uc/index";
	}
	
	/*
	 * 管理员登录
	 */
	@RequestMapping("mLogin")
	public String mLogin(Manager m,Map<String, Object>map) {
		Manager mLogin = us.MLogin(m.getName(), m.getPass());
		if(mLogin==null) {
			return "mLogin";
		}else {
			map.put("mName",mLogin.getName());
			return "mIndex";
		}
		
	}
	/*
	 * 登录
	 */
	@RequestMapping("login")
	public String login(User u,@Param("code")String code,Map<String, Object>map,HttpServletRequest request) {
		if(u.getName() != null && !"".equals(u.getName()) && u.getPass() != null && !"".equals(u.getPass())) {
			User login = us.login(u);
			if(login==null) {
				map.put("msg", "用户名和密码不正确");
				return "login";
			}else {
				//登录正确
				HttpSession session = request.getSession();
				String randomCode = (String) session.getAttribute(ValidateCode.RANDOMCODEKEY);
				if(!code.equalsIgnoreCase(randomCode)) {
					map.put("msg", "验证码有误！");
					return "login";
				}else {
					//用户名密码正确，验证码也是正确的
					session.setAttribute("name",login.getName());
					session.setAttribute("uId", login.getId());
					return "redirect:/uc/index";
				}	
			}
		}else {
			map.put("msg", "用户名和密码不能为空");
			return "login";
		}
	}
	/*
	 * 验证码
	 */
	@RequestMapping("img")
	public void img(HttpServletResponse response,HttpServletRequest request) {
		//设置相应类型，图片格式
				response.setContentType("image/jpeg");
				//设置响应头，阻止浏览器缓存此响应的内容(节约内存)
				response.setHeader("Pragma", "No-cache");
				
				//引用工具类中的方法，绘制验证码图片
				ValidateCode code = new ValidateCode();
				code.getValidateCode(request, response);//调用绘制验证码的方法，绘制图片
				
	}
	/*
	 * 去注册页面
	 */
	@RequestMapping("reg")
	public String reg() {
		return "register";
	}
	@ResponseBody
	@RequestMapping(value="findByname",produces= {"text/html; charset=UTF-8"})
	public String findByname(@RequestParam("userName")String name) {
		User u = us.findByname(name);
		if(u == null &&name!=null&&!"".equals(name)) {
			return "可用";
		}else {
			return "不可用";
		}
	}
	@ResponseBody
	@RequestMapping(value="findBytel",produces= {"text/html; charset=UTF-8"})
	public String findBytel(@RequestParam("telName")String tel) {
		User u = us.findBytel(tel);
		String reg = "1[0-9]{10}";
		if(tel != null && !"".equals(tel)) {
			if(u == null && tel.matches(reg)) {
				return "可用";
			}else {
				return "不可用";
			}
		}else {
			return "手机号不可为空";
		}
			
	}
	@ResponseBody
	@RequestMapping(value="findBypass",produces= {"text/html; charset=UTF-8"})
	public String findBypass(@RequestParam("passName")String pass) {
		if(pass != null && !"".equals(pass)) {
			if(pass.length() >= 4 && pass.length()<=12 ) {
				return "可用";
			}else {
				return "不可用";
			}
		}else {
			return "密码不可为空";
		}	
	}
	/*
	 * 提交注册
	 */
	@RequestMapping("toReg")
	public String toReg(User u,Map<String, Object>map) {
		boolean reg = us.reg(u);
		if(reg) {
			map.put("msg","注册成功请登录");
			return "login";
		}else {
			map.put("msg", "注册失败请重新注册");
			return "register";
		}
	}
}
